/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Servidor2.java
 *
 * Created on 11/06/2012, 18:03:57
 */

package principal;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import servidor1.Servidor;

//import javax.swing.ImageIcon;
/**
 *
 * @author Nico
 */
public class ServidorP extends javax.swing.JPanel {

    private PrincipalView pri;
    private Servidor conect;

    /** Creates new form Servidor2 */
    public ServidorP(PrincipalView principal) {
        pri = principal;
        initComponents();
        jLabel2.setVisible(false);
        jButton2.setVisible(false);
        this.updateUI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(360, 230));
        setMinimumSize(new java.awt.Dimension(360, 230));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(360, 230));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(principal.PrincipalApp.class).getContext().getResourceMap(ServidorP.class);
        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jTextField1.setFont(resourceMap.getFont("jTextField1.font")); // NOI18N
        jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Puerto(evt);
            }
        });

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1Iniciar(evt);
            }
        });

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jButton2.setFont(resourceMap.getFont("jButton2.font")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Detener(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)))
                .addContainerGap(32, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(83, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(46, 46, 46))
            .addGroup(layout.createSequentialGroup()
                .addGap(139, 139, 139)
                .addComponent(jButton2)
                .addContainerGap(140, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(32, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void PonerEnEspera(){
        jLabel3.disable();
        jTextField1.disable();
        jButton1.setEnabled(false);
        //javax.swing.ImageIcon imagen = new ImageIcon(jLabel2.icon);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(principal.PrincipalApp.class).getContext().getResourceMap(ClienteP.class);
        jLabel2.setIcon(resourceMap.getIcon("jLabel2.icon"));
        jLabel2.setVisible(true);
        jButton2.setVisible(true);
        updateUI();
    }

    private void IniciarServidor() {
        conect = new Servidor(Integer.valueOf(jTextField1.getText()), pri);
    }

    public void ServidorIniciado(){
        jLabel2.setText("Conexión establecida...");
        pri.ViewServidorIniciado(conect);
        updateUI();
    }

    private void jButton1Iniciar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1Iniciar
        // TODO add your handling code here:
        if(!jButton1.isEnabled()) return;
        PonerEnEspera();
        IniciarServidor();
}//GEN-LAST:event_jButton1Iniciar

    private void Puerto(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Puerto
        // TODO add your handling code here:
        PonerEnEspera();
        IniciarServidor();
    }//GEN-LAST:event_Puerto

    private void Detener(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Detener
        // TODO add your handling code here:
        conect.cancelar();
        jLabel2.setText("Esperando la conexión de un cliente...");
        jLabel3.enable();
        jTextField1.enable();
        jButton1.setEnabled(true);
        jLabel2.setVisible(false);
        jButton2.setVisible(false);
        updateUI();
    }//GEN-LAST:event_Detener


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
